using System;
using System.Windows.Forms;

using Utilities;

namespace GPX
{
    public partial class TimeDeltaDialog : FormEx
    {
        private System.Resources.ResourceManager ResourceManager
        {
            get { return GPXAppResourceManager.ResourceManager; }
        }

        public TimeDeltaDialog()
        {
            InitializeComponent();
            Utilities.Localizer.LocalizeControl(this, ResourceManager);
            try
            {
                DateTime dt = DateTime.Now;
                dateGPS.Value = dt;
                timeGPS.Value = dt;
                datePhoto.Value = dt;
                timePhoto.Value = dt;
            }
            catch
            {
                //swallow
            }
        }

        public void SetPhotoTime(DateTime photoTime)
        {
            datePhoto.Value = photoTime;
            timePhoto.Value = photoTime;
        }

        public void SetGPSTime(DateTime gpsTime)
        {
            dateGPS.Value = gpsTime;
            timeGPS.Value = gpsTime;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
            catch (Exception ex)
            {
                Utils.HandleException(ex);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                //calculate difference: the value to be added to photo time 
                //in order to get to GPS time
                DateTime dtGPS = dateGPS.Value;
                dtGPS = dtGPS.AddHours(timeGPS.Value.Hour - dtGPS.Hour);
                dtGPS = dtGPS.AddMinutes(timeGPS.Value.Minute - dtGPS.Minute);
                dtGPS = dtGPS.AddSeconds(timeGPS.Value.Second - dtGPS.Second);

                DateTime dtPhoto = datePhoto.Value;
                dtPhoto = dtPhoto.AddHours(timePhoto.Value.Hour - dtPhoto.Hour);
                dtPhoto = dtPhoto.AddMinutes(timePhoto.Value.Minute - dtPhoto.Minute);
                dtPhoto = dtPhoto.AddSeconds(timePhoto.Value.Second - dtPhoto.Second);

                _SelectedTimeSpan = dtGPS - dtPhoto;

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                Utils.HandleException(ex);
            }
        }

        private void TimeDeltaDialog_Load(object sender, EventArgs e)
        {
        }

        private TimeSpan _SelectedTimeSpan;
        public TimeSpan SelectedTimeSpan
        {
            get { return _SelectedTimeSpan; }
        }

        public bool SetAsDefault
        {
            get { return chkMakeDefault.Checked; }
        }
    }
}